/**
 * 
 */
package com.bpf.base.util.tree;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bpf.base.util.MapUtil;



/**
 * @author Administrator
 * 这是tree的类
 * 包含tree的类型转化
 */
public class Node {
	private String id;
	private String text;
	private String parNode;
	private String state;
	private String imgPath;
	private List<Node> children;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getParNode() {
		return parNode;
	}
	public void setParNode(String parNode) {
		this.parNode = parNode;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getImgPath() {
		return imgPath;
	}
	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}
	public List<Node> getChildren() {
		return children;
	}
	public void setChildren(List<Node> children) {
		this.children = children;
	}
	
	
	/**
	 * 单个node的转化
	 * @return
	 */
	public Node setNode(Map<String,Object> data,String idId,String textId,String parNodeId){
		Node node=new Node();
		node.id=data.get(idId).toString();
		node.text=data.get(textId).toString();
		node.parNode=data.get(parNodeId).toString();
		return node;
	}
	
	public Node setNode(Map<String,Object> data,String idId,String textId,String parNodeId,String state){
		Node node=new Node();
		node.id=data.get(idId).toString();
		node.text=data.get(textId).toString();
		node.parNode=data.get(parNodeId).toString();
		node.state=state.trim();
		return node;
	}
	
	/** 
	* @author 罗成
	* @date 创建时间：2016年8月4日 上午10:37:19
	* @version v1.0
	* 初始化成node类型的list,用于只有一层没有parentId的
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	*/ 
	public List<Node> setNode(List<?> datalist,String idId,String textId){
		List<Node> nodelist=new ArrayList<Node>();
		for(int i=0;i<datalist.size();i++){
			Node node=new Node();
			Class c = datalist.get(i).getClass();
			try{
				Method method = c.getMethod("get" + idId.trim().substring(0, 1).toUpperCase() + idId.trim().substring(1));
				node.id = method.invoke(datalist.get(i)).toString();
				method = c.getMethod("get" + textId.trim().substring(0, 1).toUpperCase() + textId.trim().substring(1));
				node.text = (String) method.invoke(datalist.get(i));
				nodelist.add(node);
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		return nodelist;
	}
	
	/**
	 * 初始化成node类型的list
	 */
	public List<Node> setNode(List<?> datalist,String idId,String textId,String parNodeId){
		List<Node> nodelist=new ArrayList<Node>();
		for(int i=0;i<datalist.size();i++){
			Map<String,Object> data=MapUtil.toMap(datalist.get(i));
			Node node=new Node();
			node.id=data.get(idId).toString().trim();
			node.text=data.get(textId).toString().trim();
			node.parNode=String.valueOf(data.get(parNodeId)).trim();
			nodelist.add(node);
		}
		return nodelist;
	}
	
	/** 
	 * @author 罗成
	 * @date 创建时间：2016年8月1日 上午9:44:41
	 * @version v1.0
	 * 用反射而不是MapUtil
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	*/ 
	public List<Node> setNode(List<?> datalist, String idId, String textId, String parNodeId, String stateId, String imgId) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		List<Node> nodelist=new ArrayList<Node>();
		for(Object obj : datalist){
			Node node=new Node();
			if(!StringUtils.isEmpty(idId)){
				Method method = obj.getClass().getMethod("get" + idId.substring(0, 1).toUpperCase() + idId.substring(1));
				node.id = ((String) method.invoke(obj, null)).trim();
			}
			if(!StringUtils.isEmpty(textId)){
				Method method = obj.getClass().getMethod("get" + textId.substring(0, 1).toUpperCase() + textId.substring(1));
				node.text = ((String) method.invoke(obj, null)).trim();
			}
			if(!StringUtils.isEmpty(parNodeId)){
				Method method = obj.getClass().getMethod("get" + parNodeId.substring(0, 1).toUpperCase() + parNodeId.substring(1));
				node.parNode = ((String) method.invoke(obj, null)).trim();
			}
			if(!StringUtils.isEmpty(stateId)){
				Method method = obj.getClass().getMethod("get" + stateId.substring(0, 1).toUpperCase() + stateId.substring(1));
				node.state = ((String) method.invoke(obj, null)).trim();
			}
			if(!StringUtils.isEmpty(imgId)){
				Method method = obj.getClass().getMethod("get" + imgId.substring(0, 1).toUpperCase() + imgId.substring(1));
				node.imgPath = ((String) method.invoke(obj, null)).trim();
			}
			nodelist.add(node);
		}
		return nodelist;
	}
	
	/**
	 * 数的初始化
	 * @param tList 数据结构[{'id':'','text':'','parent':''}]
	 * @returns {Array} 数据结构[{'id':'','text':'','children':'Array'}]
	 */
	public List<Node> loadTree(List<Node> tList){
		List<Node> dTree=new ArrayList<Node>();	//第一层的tree的根节点，多个树
		for(int i=0;i<tList.size();i++){
			if(tList.get(i).parNode==null||tList.get(i).parNode==""||tList.get(i).parNode=="null"||"-1".equals(tList.get(i).parNode)){
				Node node=tList.get(i);
				dTree.add(node);
				tList.remove(i);
				i--;
				nodeRecursion(node,tList);
			}
		}
		return dTree;
	}

	/**
	 * 初始化需要用的递归方法
	 * @param node 数的结点
	 * @param tList
	 */
	public void nodeRecursion(Node node,List<Node> tList){
		List<Node> childrenlist = new  ArrayList<Node>();
		node.children=childrenlist;
		for(int i=0;i<tList.size();i++){
			if(tList.get(i).parNode!=null&&tList.get(i).parNode!=""&&tList.get(i).parNode!="null"&&!"-1".equals(tList.get(i).parNode)){
				if(tList.get(i).parNode.equals(node.id)){
					Node node1=tList.get(i);
					childrenlist.add(node1);
					tList.remove(i);
					i--;
					nodeRecursion(node1,tList);
				}
			}
		}
	}
	
	/**
	 * 在一个树上重新加入节点
	 * @param treeList 
	 * @param tList
	 * @return
	 */
	public void addTree(List<Node> treeList,List<Node> tList){
		List<Node> dTree=new ArrayList<Node>();	//第一层的tree的根节点，多个树
		for(int i=0;i<treeList.size();i++){
			for(int j=0;j<tList.size();j++){
				if(treeList.get(i).getId().equals(tList.get(j).getParNode())){
					try{
						treeList.get(i).children.add(tList.get(j));
					}
					catch(Exception e){
						treeList.get(i).children=new ArrayList<>();
						treeList.get(i).children.add(tList.get(j));
						/*System.out.println("children:"+treeList.get(i).children);
						System.out.println("get(i)"+tList.get(j));*/
					}
					finally{
						
					}
					//tList.remove(j);
					//j--;
				}
			}
			if(treeList.get(i).getChildren()!=null&&treeList.get(i).getChildren().size()>0){
				addTree(treeList.get(i).getChildren(),tList);
			}
		}
	}
	
	public static void main(String[] args) {
		Integer[] arr = {1,2,3,4,5,6,7,8,9};
		List<Integer> a = new ArrayList<Integer>(Arrays.asList(arr));
		for(int i = 0; i < a.size(); i++){
			if(a.get(i)%2 == 0){
				Integer as = a.get(i);
				a.remove(i);
				System.out.println(as);
				i--;
			}
		}
	}
}
